<?php
	/**
	 * 登录
	 */
    class Logintest extends APIBase {
		
		public $tag = "Logintest";
		public $isLogin = false;
		
		public function before() {
			$this->initMysql();
			$this->initCacheRedis();
			$this->inithttp();
			return true;
		}
	
		public function logic() {
			
		
			if (!isset($this->param['user'])) {
				$this->returnError(310, "请输入用户名称");
			} 
			
			if (!isset($this->param['password'])) {
				$this->returnError(311, "请输入密码");
			}
			
			if ( !isset($this->param['vercode']) || $this->param['vercode'] == "") {
			
			} else {
				
			}

			if ( !isset($this->param['ua'])) {
				
 			} else {
 				
 			}
 						
			if (!isset($this->param['imsi'])) {				
 			} else {
 				
 			}

 			if (!isset($this->param['imei'])) {		
				
				
 			} else {
 				
				
 			}

 			if (!isset($this->param['cid'])) {			
 			} else {
 				
 			}
			
			
			$user = strtolower($this->param['user']);		 
			$row = $this->mysql->select("player", "*", array('user' => $user));
			
			
			if (count($row) == 0) {
				$this->returnError(312, '用户名称错误！');
			} elseif (count($row) > 1) {
				$this->e("{$user} repeat");
				$this->returnError(500, "用户名称重复！");
			}

			$password = md5($this->param['password'].$row[0]['salt']);
			if ($password != $row[0]['password']) {
				$this->returnError(312, '用户密码错误！');
			}
			
			if ($row[0]['status'] == 0){
				$this->returnError(310,"用户已经被禁用！");
			}
			
			$uid = $row[0]['id'];			 
			$this->initDataRedis($uid);		

			$isExists = $this->data_redis->exists("hu:{$uid}");
			$player = $this->data_redis->hMGet("hu:{$uid}", array('id', 'user','skey'));
			//$isExists = 0;
			if (!$isExists || $player['id']==null || $player['user'] == null) {
				//判断从数据库取出的会员是否过期
			    if(time()>(int)$row[0]['vip_time']){
					$row[0]['is_vip'] = 0;
				}
                //会员数据存到数据库
				$this->data_redis->hMset("hu:{$uid}", $row[0]);
				
				$games = $this->mysql->select("player_game", "*", array('user_id' => $row[0]['id']));
				if(count($games)>0){
				  foreach($games as $ga){
				   unset($ga['id']);unset($ga['user_id']);
				   $this->data_redis->hMset("hu:{$uid}:$ga[game_id]", $ga);
				  }
				}
				//道具
				/*$prop = $this->mysql->select("player_prop", "*", array('uid' => $row[0]['id']));
				if(count($prop)>0){
				  foreach($prop as $p){
				   //unset($p['id']);unset($p['user_id']);
				   $this->data_redis->hMset("hu:{$uid}:prop:$p[id]", $p);
				  }
				}*/
				
				
				$this->cache_redis->hincrBy("playing", "total", 1);//大厅人数
			}
            //$this->cache_redis->hGet('playing','total');
			
			$this->continuous_landing($uid);//计算连续登陆
			
			$salt = $this->data_redis->hGet("hu:{$uid}", 'salt');
			$now = time();
			$skey = md5($now . $salt);		
			//抢登
			if($isExists){
			    $url = 'http://'.Config::$ip_server_confing['ip'].':'.Config::$ip_server_confing['myport'].'/pushsrv?action=login&receiver='.$uid.'&param={"skey":"'.$player['skey'].'"}';
				if(!$this->http->get($url)){
				    	//$this->returnError(599, "抢登异常！");
				}
				
			} 
			//获取登录用户ip
			$ip = $this->getRealIp();
			
			//Config::$logger->info($this->tag, $uid.--.$ip);
			$this->data_redis->hMset("hu:$uid", array("update_time" => $now, "heartbeat_at" => $now, "skey" => $skey,'login_ip'=>$ip)); 
			$user_info = $this->data_redis->hMget("hu:$uid", array(
					'user', 'name', 'sex', 'money', 'coin', 'skey', 
					'login_days', 'is_get', 'total_board', 'total_win', 'exp',
					'bank', 'password', 'is_set_bank_password','avater_url',
					'gift_num1','gift_num2','gift_num3','gift_num4','charisma','activity',
					'isget_coin_task_reward_record','coin_task_complete_record',
                    'changeCardtoolNum','betsDoubletoolNum','banComparetoolNum','is_vip','vip_time','login_ip'
			));
		

			
			//获取道具
			/*$propkeys = $this->data_redis->keys("hu:{$uid}:prop:*");			
			$procount = count($propkeys);
			$user_prop = array();
			if($procount>0){

				for($prs=0;$prs<$procount;$prs++){
					$user_prop[$prs] = $this->data_redis->hGetAll($propkeys[$prs]);
				}
			
			}*/
			
			$user_info['uid'] = (int)$uid;
			$user_info['sex'] = (int)$user_info['sex'];
			$user_info['money'] = (int)$user_info['money'];
			$user_info['coin'] = (int)$user_info['coin'];
			$user_info['login_days'] = (int)$user_info['login_days'];
			$user_info['is_get'] = (int)$user_info['is_get'];
			$total_lose = $user_info['total_board'] - $user_info['total_win'];
			$user_info['history'] = array((int)$user_info['total_win'], (int)$total_lose);
			$user_info['total_board'] = (int)$user_info['total_board'];
			$user_info['total_win'] = (int)$user_info['total_win'];
			$user_info['exp'] = (int)$user_info['exp'];
			$user_info['bank'] = (int)$user_info['bank'];
			$user_info['is_vip'] = (int)$user_info['is_vip'];
			$user_info['password'] = $password;
			$user_info['is_set_bank_password'] = (int)$user_info['is_set_bank_password'];
			$user_info['gift_num1'] = (int)$user_info['gift_num1'];
			$user_info['gift_num2'] = (int)$user_info['gift_num2'];
			$user_info['gift_num3'] = (int)$user_info['gift_num3'];
			$user_info['gift_num4'] = (int)$user_info['gift_num4'];
			$user_info['charisma'] = (int)$user_info['charisma'];
			$user_info['activity'] = (int)$user_info['activity'];
			$user_info["isget_coin_task_reward_record"] = (int)$user_info['isget_coin_task_reward_record'];//是否领取奖券任务奖励记录
            $user_info['changeCardtoolNum'] = (int)$user_info['changeCardtoolNum'];
            $user_info['betsDoubletoolNum'] = (int)$user_info['betsDoubletoolNum'];
            $user_info['banComparetoolNum'] = (int)$user_info['banComparetoolNum'];
			
			$user_info['ip'] = Config::$ip_server_confing['ip'];
			$user_info['port'] = Config::$ip_server_confing['port'];
			//剩余时间 
			if((int)$user_info['is_vip']){
			  
			  $remaining = ceil(((int)$user_info['vip_time']-time())/86400);
			  $user_info['remaining'] = $remaining;
			  	
			 //会员登录通知
			  $url = 'http://'.Config::$ip_server_confing['ip'].':'.Config::$ip_server_confing['myport'].'/pushsrv?action=Marquee&receiver=all&param={"user":"'.$user_info['name'].'","type":1}';
			  if(!$this->http->get($url)){
				   // $this->returnError(599, "通知异常！");
			  }
			    
			}else{
			 //
			 /* $this->data_redis->hMset("hu:$uid",array('is_vip'=>0)); 	*/
			  $user_info['remaining'] = 0; 		
			}
			//$user_info['prop'] = $user_prop;
			
			$vga = $this->cache_redis->hGetall("vga");
			$user_info['upvenus'] = $vga['upvenus'];
			$user_info['upgoods'] = $vga['upgoods'];
			$this->returnData($user_info);
		}
	
		/**
		 * 连续登录
		 */
		private function continuous_landing($uid) {
			$ts = strtotime(date("Y-m-d"));
			$update_time = $this->data_redis->hGet("hu:{$uid}", 'update_time');
		/*	var_dump($ts);
			echo '</br>';
			var_dump($update_time);*/
			
			/*if ($update_time <= $ts) {
				$this->data_redis->hset("hu:{$uid}", "lottery_count", 0); //lottery times
				if ($update_time > ($ts - 86400)) {
					$this->data_redis->hincrBy("hu:{$uid}", "login_days", 1);
					$this->data_redis->hset("hu:{$uid}", "is_get", 0);
				} else {
					$this->data_redis->hMset("hu:{$uid}", array("login_days" => 1, "is_get" => 0));
				}
			}*/
			
			if ($update_time <= $ts) {
				$this->data_redis->hset("hu:{$uid}", "lottery_count", 0); //lottery times
				if ($update_time > ($ts - 86400)) {
					//$this->data_redis->hincrBy("hu:{$uid}", "login_days", 1);
					$this->data_redis->hset("hu:{$uid}", "is_get", 0);
				} else {
					$this->data_redis->hMset("hu:{$uid}", array("login_days" => 0, "is_get" => 0));
				}
			}
			
			
		}
		
    	public function after() {
    		$this->deinitDataRedis();
			$this->deinitCacheRedis();
			$this->deinitMysql();
			$this->deinithttp();
		}
    }
?>
